import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;
import java.net.*;
import java.lang.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class writeToFileClass {
 
  int client_id;
    Socket socket;    
    String folder;
    String vehi;
    FileOutputStream fout;
    String tempTxt=null;
    StringBuilder line=new StringBuilder();
    
    GeoFun geofun = new GeoFun();
    sendAlert sa = new sendAlert();
  
  public void makeFolder(String strFilename,String dataTxt,String vehino) throws Exception
  {
   File fos=new File(strFilename);
   FileWriter fw=new FileWriter(fos,true);
   BufferedWriter bw=new BufferedWriter(fw);
   bw.write(dataTxt);
   bw.close();
  }
   public boolean readFile(String path,String Text,String vehi1) 
   {
    String str1=null;
    try
    {
     FileInputStream fstream = new FileInputStream(path);
     DataInputStream in = new DataInputStream(fstream);
     BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
     str1= br.readLine();
     in.close();
    }
    catch (Exception e){
     //System.err.println("Error: " + e.getMessage());
     str1="";
    }
    if(str1.indexOf(Text)==-1)
    {
     return true;
    }
    else
    {
     return false;
    }
    }
    
   public void readData(String strText) throws Exception
   {
    String[] newDateTime;
    String fTxt = null;
    String subFolder=null;
    String strLine=null;
    try
    {
      if(strText.indexOf(")") > 0) 
     { 
        strText = strText.replace(")",""); 
     } 
      strText = strText.replace("$$",""); 
      String[] data1=strText.split("\\*"); 
      String[] data2=data1[0].split(",");        
      String[] data3=data1[1].split("\\|"); 
      String[] data4 = data1[0].split("U"); 
       
      String[] time1 = data4[1].split("\\."); 
      String[] time = time1[0].split("(?<=\\G.{2})"); 
      String[] date = data2[8].split("(?<=\\G.{2})"); 
       
      vehi = geofun.FilterIt(data4[0]); 
      vehi = geofun.convertStringToHex(vehi);      
       
       
      newDateTime=geofun.makeDateTime(date[0],date[1],date[2],time[0],time[1],time[2]).split(",");   
      //System.out.println("Date "+newDateTime[1]);         
 
      String latTxt = data2[2].replace(".","")+data2[3]; 
      Double latTxt1 = geofun.round(geofun.convertLat(geofun.calLat(latTxt)),8); 
      String newLat = Double.toString(latTxt1).replace(".",""); 
       
      String lngTxt = data2[4].replace(".","")+data2[5]; 
      Double lngTxt1 = geofun.round(geofun.convertLong(geofun.calLong(lngTxt)),8); 
      String newLng = Double.toString(lngTxt1).replace(".",""); 
      
      
      String otherParam = ""; 
      String IOState1 =  geofun.HexToBinary(data3[3]); 
      String[] IOState = IOState1.split("(?<=\\G.{1})");    
      
       for(int i=0; i < IOState.length; i++) 
       { 
         otherParam += "["+(i+1)+"="+IOState[i]+"] "; 
       } 
       
      if(data3.length > 7 && data3[7].toString()!="") 
      { 
        String mileStr = data3[7].substring(0,7).toString();        
        otherParam += "[mile="+Integer.parseInt(mileStr,16)+"] "; 
      } 
         
      if(data3.length > 6 && data3[6].toString()!="") 
      { 
        String CSQ = data3[6].toString();    
        otherParam += "[CSQ="+Integer.parseInt(CSQ,16)+"] "; 
      } 
       
      if(data3.length > 1 && data3[1].toString()!="") 
      { 
        String HDOP = data3[1].toString();    
        otherParam += "[HDOP="+HDOP+"] "; 
      } 
      
      if(data3.length > 4 && data3[4].toString()!="") 
      { 
        String[] analogIn1 = data3[4].split(","); 
        otherParam += "[101="+Integer.parseInt(analogIn1[0],16)+"] "; 
        if(analogIn1[1].length() > 4)
          otherParam += "[102="+Integer.parseInt(analogIn1[1].substring(0,4),16)+"] ";
        else
          otherParam += "[102="+Integer.parseInt(analogIn1[1],16)+"] ";
                  
      } 
       
      //System.out.println("Data "+data3[3]+" "+Integer.parseInt("3",16)+" "+IOState1);      
       
      long speed1 = Math.round(Double.parseDouble(data2[6])* 1.609344); 
     
      fTxt="@"+vehi+","+newLng+","+newLat+","+speed1+","+data2[7]+","+data3[2]+",0,"+otherParam+","+newDateTime[0]+","+newDateTime[1]+"#";   
      //System.out.println("ss"+fTxt+"\n");      
      
      //subFolder="E:/xampp/htdocs/src/data/"+newDateTime[0]+"/";
      subFolder="/opt/lampp/htdocs/src/data/"+newDateTime[0]+"/";
      //System.out.println("subFolder "+subFolder);
      File sub = new File(subFolder);
      try
      {
        if(sub.mkdirs()){}
        else{}
      }
      catch(Exception e)
      {
        e.printStackTrace();
      }
      subFolder+=vehi+".txt";
      //System.out.println(subFolder);      
      
      if(readFile(subFolder,fTxt,vehi))
      {
        //System.out.println("Data "+fTxt);
        makeFolder(subFolder,fTxt,vehi);
        sa.sendSMSAlert(fTxt);
      } 
      

    }   
    catch(Exception e)
    {
       //System.out.println("Problem finding file");
    }
  }
 

 /*public static void main (String[] args) 
 {
   writeToFileClass wtf= new writeToFileClass();
   try
   {
    //wtf.readData("$CHD5991,27,07,09,10,10,01,17266264N,078260380E,00.0,164#$CHD5991,27,07,09,10,10,21,17266264N,078260380E,00.0,178#");
    wtf.readData("$$Y#5IU074706.000,A,2841.9437,N,07706.8877,E,0.00,215,301011,,*01|1.6|219|2000|0010,000F|0194000A008CC103|1F|0002CB0BS");
   }
   catch(Exception e)
   {
    
   }
 }*/
    
}
